<style>
.config {
  padding: 10px;
}

.el-form-item__content {
  text-align: left;
}
</style>
<template>
  <section class="config">
    <el-form ref="form" :model="config"
             label-width="100px">
      <el-form-item label="自动做T">
        <el-switch v-model="config.autoT"
                   :active-value="1"
                   :inactive-value="0"
                   @change="setConfigs('autoT',config.autoT)"></el-switch>
      </el-form-item>
      <el-form-item label="最大做T持仓">
        <el-input v-model="config.maxT"
                  @blur="setConfigs('maxT',config.maxT)"
        ></el-input>
      </el-form-item>
      <el-form-item label="自动定投">
        <el-switch v-model="config.autoDT"
                   :active-value="1"
                   :inactive-value="0"
                   @change="setConfigs('autoDT',config.autoDT)"></el-switch>
      </el-form-item>
      <el-form-item label="最大定投持仓">
        <el-input v-model="config.maxDT"
                  @blur="setConfigs('maxDT',config.maxDT)"
        ></el-input>
      </el-form-item>
      <el-form-item label="每天买代码">
        <el-input v-model="config.code"
                  @blur="setConfigs('code',config.code)"
        ></el-input>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
import {getConfig, setConfig} from '../utils/request'

export default {
  data() {
    return {
      config: {
        autoT: "",
        maxT: 1,
        autoDT: "",
        maxDT: 1,
        code: ""
      },
    }
  },
  created() {
    this.getConfigs()
  },
  methods: {
    getConfigs() {
      getConfig().then((res) => {
        if (res.status) {
          this.config = res.content
        } else {
          this.$message.error(res.content);
        }
      })
    },
    setConfigs(name, value) {
      setConfig({
        name,
        value
      }).then(() => {
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      });
    },
  }
}
</script>
